<?php 
namespace Efy\Db;
/**
 * @see adodb.inc.php
 * Call Adodb library
 */
require_once (__DIR__ . '/../adodb/adodb.inc.php');
//require_once(__DIR__ . '/Db/Connection.php');
/**
 *
 */
class Connection {	
	protected static $_instance = null;
 	public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
	/**
	 * 
	 */
	public static function connectADO($adapter, $config = array()){		
		global $adoConn, $ADODB_CACHE_DIR, $ADODB_CACHE_TIMEOUT;
		if($adapter == "MSSQL"){//Ket noi MS SQL server
			//Tao doi tuong ADODB
			$adoConn = NewADOConnection("ado_mssql");  // create a connection
			$connStr = "Provider=SQLOLEDB; Data Source=" . $config['host'] . ";Initial Catalog='" . $config['dbname'] . "'; User ID=" . $config['username'] . "; Password=" .$config['password'];
			//call connect adodb
			$adoConn->Connect($connStr) or die("Hien tai he thong khong the ket noi vao CSDL duoc!");
		}
		$ADODB_CACHE_DIR  		= $config['pathAdoCache'];
		$ADODB_CACHE_TIMEOUT 	= $config['cachetimeout'];
		return $adoConn;
	}
	
	/**
	 * Creater: HUNGVM
	 * Date: 
	 * Thuc thi hanh dong update / delete / getsingle / ...
	 * @param $sql : Xau SQL can thuc thi
	 * @return unknown
	 */
	public function adodbExecSqlString($sql){
		global $adoConn;
		$adoConn->SetFetchMode(ADODB_FETCH_ASSOC);
		$ArrSingleData = $adoConn->GetRow($sql); 
		return $ArrSingleData;
	}
	
	/**
	 * Creater: HUNGVM
	 * date:
	 * Lay tat ca thong tin trong CSDL, phan tu dang chi so bat dau tu 0,1,2,...
	 * @param $sql : Xau SQL can thuc thi
	 * @param $optCache : Tuy chon co cache hay khong? <> "" thi thuc hien cache
	 * @return Mang luu thong tin du lieu
	 */
	public function adodbQueryDataInNumberMode($sql, $optCache = ""){
		global $adoConn;
		//Thoi gian Cache
		$adoConn->SetFetchMode(ADODB_FETCH_NUM);
		if ($optCache == ""){
			//echo "1";
			$ArrAllData = $adoConn->GetArray($sql); 
		}else{
			global $ADODB_CACHE_TIMEOUT;
			$cacheTime = $ADODB_CACHE_TIMEOUT;
			$ArrAllData = $adoConn->CacheGetAll($cacheTime,$sql); 
		}
		return $ArrAllData;
	}
	
	/**
	 * Creater: HUNGVM
	 * date:
	 * Lay tat ca thong tin trong CSDL, phan tu dang ten cot
	 * @param $sql : Xau SQL can thuc thi
	 * @param $optCache : Tuy chon co cache hay khong? <> "" thi thuc hien cache
	 * 			
	 * @return Mang luu thong tin du lieu
	 */
	public function adodbQueryDataInNameMode($sql, $optCache = ""){
		global $adoConn;
		//Thoi gian Cache
		$adoConn->SetFetchMode(ADODB_FETCH_ASSOC);
		if ($optCache == ""){
			$ArrAllData = $adoConn->GetArray($sql); 
		}else{
			global $ADODB_CACHE_TIMEOUT;
			$cacheTime = $ADODB_CACHE_TIMEOUT;
			$ArrAllData = $adoConn->CacheGetAll($cacheTime,$sql); 
		}
		return $ArrAllData;
	}

	public function exportSql($arrParameter){
		$sql ='';
		if(is_array($arrParameter)){
			foreach ($arrParameter as $key => $value) {
				if($sql !=''){
					$sql .= ",'" . $value . "'";
				}else{
					$sql .= "'" . $value . "'";
				}
			}
		}
		return $sql;
	}
	/**
	 * @author: Đặng Văn Trường
	 * @since: 25/12/2012
	 * @todo : Ham thuc hien query du lieu
	 * @param : $arrParameter: Mang du lieu truyen vao csdl
	 * @param : $spName: Ten procedure
	 * @param : $optionQuery: Loai query
	 * @param : $optionReturn: kieu tra ve: true trả về chuỗi sql chưa đc query, false tra ve de lieu dc query
	 */
	public function EfyLibQueryDb($arrParameter,$spName,$optionQuery = true,$optionReturn = false){		
		$arrResult = array();
		$sql = $spName.' '.$this->exportSql($arrParameter);
		if($optionReturn){
			return $sql;
		}
		try{
			if($optionQuery){
				$arrResul = $this->adodbQueryDataInNameMode($sql);
			}else{
				$arrResul = $this->adodbExecSqlString($sql);
			}
		}catch (Exception $e){
			echo $e->getMessage();
		}		
		return $arrResul;
	}
}